using System;
using System.Collections.Generic;
using System.Text;

namespace GoogleNet
{
    /// <summary>
    /// Defines a connected set of line segments. Use LineStyle to specify the color, color mode, 
    /// and width of the line. 
    /// When a LineString is extruded, the line is extended to the ground, forming a polygon 
    /// that looks somewhat like a wall. For extruded LineStrings, the line itself uses the current 
    /// LineStyle, and the extrusion uses the current PolyStyle.
    /// </summary>
    public class KmlLineString : KmlGeometry, IKmlElement, IKmlGeometry
    {
        private string tagName = "LineString";

        #region Coordinates
        private List<KmlPointCoordinates> coordinates = new List<KmlPointCoordinates>();

        /// <summary>
        /// Two or more coordinate tuples, each consisting of floating point values for longitude, 
        /// latitude, and altitude. The altitude component is optional. Insert a space between tuples. 
        /// Do not include spaces within a tuple. 
        /// </summary>
        /// <value>The coordinates.</value>
        public List<KmlPointCoordinates> Coordinates
        {
            get { return coordinates; }
            set { coordinates = value; }
        }
        #endregion

        #region IKmlElement Members

        /// <summary>
        /// Name of the KML tag for this specific element.
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        string IKmlElement.TagName
        {
            get { return tagName; }
        }

        #endregion

        #region IKmlGenerator Members

        /// <summary>
        /// Generates the KML for this element including its child elements.
        /// </summary>
        /// <param name="writer">The XML writer that will be used to generate the KML.</param>
        public override void GenerateKml(System.Xml.XmlTextWriter writer)
        {
            writer.WriteStartElement(tagName);
            base.GenerateKml(writer);

            // write coordinates
            if (coordinates.Count >= 2)
            {
                StringBuilder coordsBuilder = new StringBuilder();
                for (int i = 0; i < coordinates.Count; i++)
                {
                    coordsBuilder.Append(coordinates[i].ToString());
                    if(i < coordinates.Count-1)
                        coordsBuilder.Append(" ");
                }

                writer.WriteElementString("coordinates", coordsBuilder.ToString());
            }
            else
            {
                throw new InsuficientPointCoordinatesException("LineString element must have 2 or more points coordinates!");
            }
            
            writer.WriteFullEndElement();
        }

        #endregion
    }
}
